<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    private const string SCRIPT_TEMPLATE = "<" + "script " + "type=\"text/javascript\">window.parent.photoUploadComplete('{0}', {1});" + "<" + "/script" + ">";

    private void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            //Sleeping for 10 seconds, fake delay, You should not it try at home.
            System.Threading.Thread.Sleep(10 * 1000);
            UploadPhoto();
        }
    }

    private void UploadPhoto()
    {
        string script = string.Empty;

        if ((filPhoto.PostedFile != null) && (filPhoto.PostedFile.ContentLength > 0))
        {
            if (!IsValidImageFile(filPhoto))
            {
                script = string.Format(SCRIPT_TEMPLATE, "The uploaded file is not a valid image file.", "true");
            }
        }
        else
        {
            script = string.Format(SCRIPT_TEMPLATE, "Please specify a valid file.", "true");
        }

        if (string.IsNullOrEmpty(script))
        {
            //Uploaded file is valid, now we can do whatever we like to do, copying it file system,
            //saving it in db etc.

            //Your Logic goes here

            script = string.Format(SCRIPT_TEMPLATE, "Photo uploaded.", "false");
        }

        //Now inject the script which will fire when the page is refreshed.
        ClientScript.RegisterStartupScript(this.GetType(), "uploadNotify", script);
    }

    private static bool IsValidImageFile(HtmlInputFile file)
    {
        return true;
        try
        {
            using (Bitmap bmp = new Bitmap(file.PostedFile.InputStream))
            {
                return true;
            }
        }
        catch (ArgumentException)
        {
            //throws exception if not valid image
        }

        return false;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Photo Upload</title>
    <style type="text/css">
        body,input,form,span,div{font-family:Tahoma;font-size:8pt;}
    </style>
</head>
<body style="margin:0px">
<form id="photoUpload" enctype="multipart/form-data" runat="server">
    <div>
        <input id="filPhoto" type="file" runat="server"/>
    </div>
    <div id="divUpload" style="padding-top:4px">
        <input id="btnUpload" type="button" value="Upload Photo" />
    </div>
</form>
</body>
</html>